package com.daxia.bumall.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.daxia.bumall.common.SmsSendType;
import com.daxia.bumall.dao.DesignerDAO;
import com.daxia.bumall.dao.UserDAO;
import com.daxia.bumall.dto.DesignerDTO;
import com.daxia.bumall.model.Designer;
import com.daxia.bumall.model.User;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;
import com.daxia.bumall.util.SmsService;
import com.daxia.bumall.util.SpringSecurityUtils;
import com.daxia.bumall.util.ValidationUtils;

@Service
public class DesignerService {
	
	@Autowired
	private DesignerDAO designerDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private UserService userService;
	
	public List<DesignerDTO> find(DesignerDTO query, Page page) {
		List<Designer> models = designerDAO.find(query, page);
		List<DesignerDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public DesignerDTO findByUser(Long userId) {
		DesignerDTO query = new DesignerDTO();
		query.setUser(new User(userId));
		return this.findOne(query);
	}
	
	public List<DesignerDTO> findAll() {
		return this.find(new DesignerDTO(), null);
	}
	
	public List<DesignerDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new DesignerDTO(), page);
	}
	
	public Long create(DesignerDTO dto) {
		Designer model = new Designer();
		toModel(model, dto);
		
		ValidationUtils.assertTrue(model.getUser() != null, "相关用户不存在");
		ValidationUtils.assertTrue(model.getUser().getCity() != null, "关联用户城市不能为空");
		
		Long id = designerDAO.create(model);
		
		model = designerDAO.load(id);
		User user = userDAO.load(model.getUser().getId());
		if (user.getParent() == null && SpringSecurityUtils.getCurrentUser() != null) {
			user.setParent(userService.findCityOperatorByCity(model.getUser().getCity().getId()));
			userDAO.update(user);
		}
		return id;
	}
	
	public DesignerDTO load(Long id) {
	    Designer model = designerDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(DesignerDTO dto) {
		Designer model = designerDAO.load(dto.getId());
		toModel(model, dto);
		designerDAO.update(model);
    }
	
	public void updateForAudit(DesignerDTO dto) throws Exception {
		Designer model = designerDAO.load(dto.getId());
		model.setAuditRemark(dto.getAuditRemark());
		model.setAuditStatus(dto.getAuditStatus());
		model.setAuditTime(dto.getAuditTime());
		model.setAuditUser(dto.getUser());
		SmsService.sendWithTemplate(model.getUser().getMobile(), SmsSendType.DesignerAudited.getValue(), new HashMap<String, Object>());
		designerDAO.update(model);
    }
	
	@Transactional
	public void auditAll(Long[] ids, int auditStatus) throws Exception {
		User user = (User) SpringSecurityUtils.getCurrentUser();
		for (Long id : ids) {
			Designer d = designerDAO.load(id);
			d.setAuditStatus(auditStatus);
			d.setAuditUser(user);
			d.setAuditTime(new Date());
			designerDAO.update(d);
			SmsService.sendWithTemplate(d.getUser().getMobile(), SmsSendType.DesignerAudited.getValue(), new HashMap<String, Object>());
		}
	}
	
	public void updateForCompany(DesignerDTO dto) {
		Designer model = designerDAO.load(dto.getId());
		model.setCompanyUser(dto.getCompanyUser());
		designerDAO.update(model);
	}

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				designerDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public DesignerDTO findOne(DesignerDTO query) {
		Designer model = designerDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<DesignerDTO> toDTOs(List<Designer> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<DesignerDTO>(0);
		}
		List<DesignerDTO> dtos = new ArrayList<DesignerDTO>(models.size());
		for (Designer model : models) {
	        DesignerDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private DesignerDTO toDTO(Designer model) {
		if (model == null) {
			return null;
		}
		DesignerDTO dto = BeanMapper.map(model, DesignerDTO.class);
		
		return dto;
	}
	
	private void toModel(Designer model, DesignerDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<Designer> toModels(List<DesignerDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<Designer>(0);
		}
		List<Designer> models = new ArrayList<Designer>(dtos.size());
		for (DesignerDTO dto : dtos) {
	        Designer model = new Designer();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}

	/**
	 * 根据公司关联的用户，去查找这个公司下的设计师
	 * @param id
	 * @return
	 */
	public List<DesignerDTO> findByCompanyUserId(Long id) {
		// TODO Auto-generated method stub
		return null;
	}
}
